<?php

namespace app\api\controller;

use app\api\model\UserModel;
use think\Cache;
use app\api\service\Token;
use think\Db;
use think\Request;
use app\api\common\sendmsg\SendM;

class User extends BaseController
{
    // 保存用户的基本信息
    public function saveUserInfo()
    {
//        Cache::store('redis')->clear();
        $param = input('post.');
        $user = new UserModel();
        $token = new Token();
        $uid = $token::getCurrentTokenVar('uid');
        $flag = $user->updateUserInfo($param,$uid);
        return json($flag);
    }

    //返回用户基本信息
    public function getUserInfo()
    {
        $param = Request::instance()->get('openid');
        $res = Db::name('iuser')->where('openid',$param)->find();
        if ($res){
            return json(msg(1,$res,'获取用户信息成功'));
        }else{
            return json(msg(0,'','获取用户信息失败'));
        }
    }

    public function telLogin()
    {
        $param = Request::instance()->only(['tel','name','code']);
        $validate = validate('LoginValidate');
        if(!$validate->scene('login')->check($param)){
            return json(['code'=>'0','msg'=>$validate->getError()]);
        }
        $code = Cache::get($param['tel']);
        if ($code == $param['code']){
            $user = new UserModel();
            $token = new Token();
            $uid = $token::getCurrentTokenVar('uid');
            $flag = $user->updateUserInfo($param,$uid);
            return json($flag);
        }else{
            return json(msg(0,'','验证码不正确'));
        }


    }


    // 点击发送验证码
    public function dialogRegister(Request $request)
    {
        $param=input('post.');
        $validate = validate('LoginValidate');
        if(!$validate->scene('vertify')->check($param)){
            return json(['code'=>'400','msg'=>$validate->getError()]);
        }
//        if (Cache::has($param['tel'].'yzm')){
//            return json(['code'=>'0','msg'=>'操作频繁,请稍后再试']);
//        }else{
//            Cache::set($param['tel'].'yzm','yzm',60);
//        }
//        Cache::inc($request->ip());
//        if (Cache::get($request->ip())>'5'){
//            Cache::set($request->ip(),Cache::get($request->ip()),1);
//            return json(['code'=>'0','msg'=>'操作次数过多,请稍后再试']);
//        }
        $send = new SendM();
        $rand = rand(100000,999999);
        Cache::set($param['tel'],$rand,1800);
        $data='【商标查询大师】您的验证码是：'.$rand.'，请在30分钟内输入，如非本人操作请忽略。';
        $status = $send->sendVertify($data,$param['tel']);
        $status=json_decode($status,true);
        if (!$status['status']=='success')
        {
            return json(['code'=>'0','msg'=>'获取短信验证码失败']);
        }else{
            return json(['code'=>'1','msg'=>'发送成功']);
        }
    }

    /**
     * 修改用户信息
     */
    public function userEdit()
    {
        $param =Request::instance()->only(['sex','birthday']);
        $token = new Token();
        $uid = $token::getCurrentTokenVar('uid');
        $res = Db::name('iuser')->where('id',$uid)->update($param);
        if ($res){
            return json(msg(1,'','修改成功'));
        }else{
            return json(msg(0,'','修改失败'));
        }
    }

//    public function cs()
//    {
//        $param = input('param.');
//        $param['add_time']=time();
//        Db::name('iuser')->insertGetId($param);
//        var_dump($param);die;
//    }
}
